package com.leyou.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.TbSpecGroup;
import com.leyou.item.entity.TbSpecParam;
import com.leyou.item.service.TbSpecGroupService;
import com.leyou.item.service.TbSpecParamService;
import com.leyou.item.service.impl.Attribute.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 属性组
 * */

@RestController
@RequestMapping("/spec")
public class AttributeSpecController {
    @Autowired
    private TbSpecParamService specParamService;
    @Autowired
    private TbSpecGroupService specGroupService;
    /*
    * 分类（手机类）ID找参数分组
    * */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> findSpecGroupByCategoryId(@RequestParam("id") Long categoryId) {
        List<SpecGroupDTO> list = specGroupService.findSpecGroupByCategoryId(categoryId);
        return ResponseEntity.ok(list);
    }
    /*
    * 分类（手机类）ID或参数分组ID找参数
    * */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> findSpecParamByGroupId( @RequestParam(value = "searching", required = false) Boolean searching,@RequestParam(name = "gid", required = false) Long groupId, @RequestParam(name = "cid", required = false) Long cid) {
        List<SpecParamDTO> list = specGroupService.findSpecParamByGroupId(groupId, cid,searching);
        return ResponseEntity.ok(list);
    }
    /*
    * 添加商品分类的参数组
    * */
    @PostMapping("/group")
    public ResponseEntity<Void> addGroup(@RequestBody TbSpecGroup group) {
        specGroupService.addGroup(group);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
    @Autowired
    private SpecificationService specService;
    /**
     * 查询规格参数组，及组内参数
     * @param id 商品分类id
     * @return 规格组及组内参数
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> findSpecsByCid(@RequestParam("id") Long id){
        return ResponseEntity.ok(specService.findSpecsByCid(id));
    }
    /*
    * 修改商品分类的参数组
    * */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody TbSpecGroup group) {
        specGroupService.updateGroup(group);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /*
     * 删除商品分类的参数组及相关参数
     * */
    @DeleteMapping("/group/{gid}")
    public ResponseEntity<Void> deleteGroupById(@PathVariable(name = "gid") String id) {
        specGroupService.deleteGroupById(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    /*
    * 添加商品分类的参数组的参数
    * */
    @PostMapping("/param")
    public ResponseEntity<Void> addParam(@RequestBody TbSpecParam param){
       specParamService.addParam(param);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /*
    * 删除商品分类的参数组的某个参数
    * */
    @DeleteMapping("/param/{pid}")
    public ResponseEntity<Void> deleteParamById(@PathVariable(name = "pid") String id) {
        specParamService.deleteParamById(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    /*
    * 修改某个参数
    * */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody TbSpecParam param){
        specParamService.updateParam(param);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
